package cn.com.inossem.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.com.inossem.model.RetrieveInfoModel;
import cn.com.inossem.utils.Utils;

public class CityDialogActivity extends Activity implements OnClickListener {

	ImageView back;
	LinearLayout linlayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.cityactivity_dialog);
		back = (ImageView) this.findViewById(R.id.back);
		back.setOnClickListener(this);
		linlayout = (LinearLayout) this.findViewById(R.id.linlayout);
		addView(linlayout);
	}

	public void refreshLayout() {
		linlayout.removeAllViews();
		addView(linlayout);
	}

	private void addView(LinearLayout layer) {
		List<View> listtextview = new ArrayList<View>();

		for (int i = 0; i < CityActivity.xuanzhongCity.size(); i++) {
			if (CityActivity.xuanzhongCity.get(0).getIsselected() == -1)
				continue;
			LayoutInflater mInflater = LayoutInflater
					.from(CityDialogActivity.this);
			View view = mInflater.inflate(R.layout.textviewitem, null);
			String text = CityActivity.xuanzhongCity.get(i).getName();
			TextView textview = (TextView) view.findViewById(R.id.textview);
			textview.setBackgroundResource(R.drawable.weixuanzhong);
			textview.setText(text);
			textview.setTag(CityActivity.xuanzhongCity.get(i));

			textview.setTextColor(Color.rgb(66, 66, 66));
			textview.setBackgroundResource(R.drawable.weixuanzhong);

			textview.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					TextView tv = (TextView) v;
					RetrieveInfoModel rm = (RetrieveInfoModel) tv.getTag();
					CityActivity.xuanzhongCity.remove(rm);
					CityActivity.delCity.add(rm);
					refreshLayout();
				}
			});

			Paint paint = new Paint();
			paint.setTextSize(textview.getTextSize());
			float size = paint.measureText(textview.getText().toString());
			view.setTag((int) size
					+ Utils.dip2px(CityDialogActivity.this, 10 * 2 + 10));

			listtextview.add(view);
		}
		int j = 0;
		LinearLayout llayout = null;
		int MaxWidth = MainActivity.widthPixels
				- Utils.dip2px(CityDialogActivity.this, 20);
		int temporarywidth = 0;
		while (true) {
			View view = null;
			int tagwidth = 0;
			if (j == listtextview.size()) {
				return;
			} else {
				view = listtextview.get(j);

				tagwidth = (Integer) view.getTag();
			}
			if (j == 0) {
				llayout = new LinearLayout(CityDialogActivity.this);
			}
			System.out.println("textview:" + tagwidth + "temporarywidth:"
					+ temporarywidth + "MaxWidth:" + MaxWidth);
			if (tagwidth + temporarywidth < MaxWidth) {
				temporarywidth += tagwidth;
				llayout.addView(view);
			} else {
				layer.addView(llayout);

				LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
						LinearLayout.LayoutParams.MATCH_PARENT,
						LinearLayout.LayoutParams.WRAP_CONTENT);
				layoutParams.setMargins(0, 15, 0, 0);

				llayout = new LinearLayout(CityDialogActivity.this);
				llayout.setLayoutParams(layoutParams);
				temporarywidth = (Integer) view.getTag();

				llayout.addView(view);
			}

			if (j == listtextview.size() - 1) {
				layer.addView(llayout);
			}
			j++;
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			setResult(1);
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			setResult(1);
			finish();
		}

		return false;

	}
}
